---
category: String
created: '2020-06-07'
title: Strip ANSI codes from a string
updated: '2021-10-13'
---

**JavaScript version**

```js stripAnsiCodes.js
const stripAnsiCodes = (str) =>
    str.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');
```

**TypeScript version**

```ts stripAnsiCodes.ts
const stripAnsiCodes = (str: string): string =>
    str.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');
```

**Examples**

```js examples.js
stripAnsiCodes('\u001B[4mcake\u001B[0m'); // 'cake'
stripAnsiCodes('\u001B[0m\u001B[4m\u001B[42m\u001B[31mfoo\u001B[39m\u001B[49m\u001B[24mfoo\u001B[0m'); // 'foofoo'
```
